{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入库 (cgh 库计划有对应的 cli 版本, 但是目前还没写. 直接从 python 使用没有问题)\n",
    "from hduq.cgh import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 实例化 CGH\n",
    "cgh = CGH(103) # 103um 表示特征宽度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 往该实例中添加模式, 模块采用惰性计算, 先添加要分解的模式再进行计算\n",
    "cgh.add_modes(HG(0, 1), 500, 0) # 500, 0 分别为 x 和 y 方向的载波频率, 是一个相对值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CGH not generated. Running cal() automatically...\n"
     ]
    }
   ],
   "source": [
    "# 生成的 cgh 预览 (未执行计算会自动调用 cal 方法计算)\n",
    "cgh.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 正负模式的生成方法\n",
    "P = HG(0, 0) + HG(0, 1) # 正模式\n",
    "M = HG(0, 0) - HG(0, 1) # 副模式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 可以产生很复杂的叠加模式 (归一化系数自动为等权重叠加)\n",
    "_ = HG(0, 0) + HG(0, 1) + HG(0, 2) + HG(0, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 同时模式分解\n",
    "cgh = CGH(103) # 替换掉原来 CGH 实例, 因为现在要产生一副新的 CGH 了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 传入要分解的模式\n",
    "cgh.add_modes([P, M], [500, 500], [-50, 50]) # 一次性传入\n",
    "\n",
    "# 也可以分两次传入\n",
    "# cgh.add_modes(P, 500, -50)\n",
    "# cgh.add_modes(M, 500, 50)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CGH not generated. Running cal() automatically...\n"
     ]
    }
   ],
   "source": [
    "# 直接读取结果或者保存\n",
    "cgh_array = cgh.result()\n",
    "# cgh.save('your_cgh.bmp')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
